/* Janus Chat - Version 1.0
 * Authors: Matthew Perry
 *          Michael Freid
 *          Marc Latou
 *          Richard Lee
 *
 * The Janus Chat Client is a highly secured
 * peer-to-peer chat client designed to facilitate
 * the transfer of sensitive data or as a communication
 * medium for the extra paranoid
 *
 * This file contains the implementation for the DirectTab
 * QT class which facilitates the input of data for direct
 * connections */

#include "directtab.h"
#include "connecttab.h"
#include "chattab.h"
#include "ui_directtab.h"
#include <QRegExp>
#include <QHostAddress>

/* The DirectTab constructor
 * This constructor initializes the UI,
 * connects some signals and slots and
 * installs the enter key filter to its
 * inputs */
DirectTab::DirectTab(User* user, QWidget *parent) :
    DetailTab(user,parent),
    ui(new Ui::DirectTab)
{
    //Initialize all of the UI Elements (Widgets) for this class
    ui->setupUi(this);

    //Establish a signal and slot connection for the buttons on this class
    connect( ui->buttonOk, SIGNAL(clicked()), this, SLOT(buttonOk_clicked()) );
    connect( ui->buttonBack, SIGNAL(clicked()), this, SLOT(buttonBack_clicked()) );

    //Install an event filter for pressing the enter button
    ui->lineIP->installEventFilter( this );
    ui->linePort->installEventFilter( this );
}

/* Destructor */
DirectTab::~DirectTab()
{
    delete ui;
}

/* The connectSocket function:
 * This function is an override of the pure
 * virtual function from the DetailTab class
 * it simply describes a means to connect directly */
void DirectTab::connectSocket( QTcpSocket* sock )
{
    //To directly connect, simply call connect with the address and port
    sock->connectToHost( ui->lineIP->text(), ui->linePort->text().toInt() );
}

/* The getAddress function:
 * Simply gives the detailtab super class
 * the ability to see the address that the
 * user enters */
QString DirectTab::getAddress()
{
    return ui->lineIP->text();
}

/* The beginChat function:
 * Implements the detailtab's function
 * which needs to start a new chat */
void DirectTab::beginChat()
{
    //Initialize a new ChatTab class
    ChatTab* dt = new ChatTab( myUser, mySock, true, myTabWidget );
    //Insert a new tab into the tabWidget using the ChatTab widget because the ok button was clicked
    int n = myTabWidget->insertTab( myTabWidget->currentIndex(), dt, mySock->peerAddress().toString() );
    //Remove the old tab because we have moved on
    myTabWidget->removeTab( myTabWidget->currentIndex() );
    //Use slot to change the current tab index
    myTabWidget->setCurrentIndex( n );
}

/* the checkInput function:
 * This function is an override of the pure
 * virtual function from the DetailTab class
 * it ensures that the input is valid */
QString DirectTab::checkInput()
{
    QString retval;
    //Check IP
    QString ipAddr = ui->lineIP->text();
    if( ipAddr.isEmpty() )
        retval.append( "* You must enter an IP Address\n" );
    else
    {
        //Validate IP
        QRegExp ip( "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" );
        QRegExp host( "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9-]*[a-zA-Z0-9]).)*([A-Za-z]|[A-Za-z][A-Za-z0-9-]*[A-Za-z0-9])$" );
        if( !ip.exactMatch( ipAddr ) && !host.exactMatch( ipAddr ) )
            retval.append( "* Invalid IP Address or Hostname\n" );
    }

    //Check Port
    QString portStr = ui->linePort->text();
    if( portStr.isEmpty() )
        retval.append( "* You must enter a Port Number\n" );
    else
    {
        //Validate Port
        bool isNumber;
        int port = portStr.toInt( &isNumber );
        if( !isNumber )
            retval.append( "* Invalid Port Number\n" );
        else
        {
            //Ensure Port is in a valid range
            if( port > 65535 )
                retval.append( "* Port Number must be less than 65535\n" );
        }
    }

    return retval;
}
